<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <script>

        // promise 的结果一旦确定，就不会再改变！！！！！！！！！！！！



        const p = new Promise((resolve, reject) => {
            const xhr = new XMLHttpRequest();
            xhr.open('get', '../php/1.php', true);
            xhr.send();
            xhr.onreadystatechange = function () {
                console.log(xhr.readyState);  // 2 3 4
                if (xhr.readyState === 4 && xhr.status === 200) {
                    let data = xhr.responseText;
                    resolve(data);
                }
                // else {
                //     reject('请求失败');   // 此处一旦写了else  readyState是2的时候 就会执行请求失败  promise的结果就确定，后面也不会再改变
                // }
            }
        })

        p.then(
            res => {
                console.log(res);
            },
            err => {
                console.log(err);
            }
        )

    </script>

</body>

</html>